<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>局部变量和全局变量 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter08/section02.html" />
    
    
    <link rel="prev" href="../chapter08/index.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="8.1"
        data-chapter-title="局部变量和全局变量"
        data-filepath="chapter08/section01.md"
        data-basepath=".."
        data-revision="Sun Feb 09 2020 13:02:11 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="../chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        什么是函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="../chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        函数的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="../chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        函数的参数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="../chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        函数的返回值
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="../chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        文档注释
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="../chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        函数练习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="../chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        函数调用函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08/index.html">
            
                
                    <a href="../chapter08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        函数高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="8.1" data-path="chapter08/section01.html">
            
                
                    <a href="../chapter08/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        局部变量和全局变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="chapter08/section02.html">
            
                
                    <a href="../chapter08/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        函数返回值详解
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="chapter08/section03.html">
            
                
                    <a href="../chapter08/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        函数参数详解
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="chapter08/section04.html">
            
                
                    <a href="../chapter08/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        可变数据类型和不可变数据类型
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x5C40;&#x90E8;&#x53D8;&#x91CF;">&#x5C40;&#x90E8;&#x53D8;&#x91CF;</h2>
<ul>
<li>&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x5C31;&#x662F;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;</li>
<li>&#x5176;&#x4F5C;&#x7528;&#x8303;&#x56F4;&#x662F;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5185;&#x90E8;&#xFF0C;&#x5373;&#x53EA;&#x80FD;&#x5728;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x7684;&#x5916;&#x90E8;&#x662F;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x7684;</li>
<li>&#x56E0;&#x4E3A;&#x5176;&#x4F5C;&#x7528;&#x8303;&#x56F4;&#x53EA;&#x662F;&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x51FD;&#x6570;&#x5185;&#x90E8;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x540C;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x76F8;&#x540C;&#x540D;&#x5B57;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF08;&#x6253;&#x4E2A;&#x6BD4;&#x65B9;&#xFF0C;&#x628A;&#x4F60;&#x3001;&#x6211;&#x662F;&#x5F53;&#x505A;&#x6210;&#x51FD;&#x6570;&#xFF0C;&#x628A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7406;&#x89E3;&#x4E3A;&#x6BCF;&#x4E2A;&#x4EBA;&#x624B;&#x91CC;&#x7684;&#x624B;&#x673A;&#xFF0C;&#x4F60;&#x53EF;&#x6709;&#x4E2A;iPhone8&#xFF0C;&#x6211;&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x6709;&#x4E2A;iPhone8&#x4E86;&#xFF0C; &#x4E92;&#x4E0D;&#x76F8;&#x5173;&#xFF09;</li>
<li>&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#xFF0C;&#x4E3A;&#x4E86;&#x4E34;&#x65F6;&#x4FDD;&#x5B58;&#x6570;&#x636E;&#x9700;&#x8981;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#x6765;&#x8FDB;&#x884C;&#x5B58;&#x50A8;</li>
<li>&#x5F53;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x88AB;&#x521B;&#x5EFA;&#xFF0C;&#x5F53;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5B8C;&#x6210;&#x540E;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x5C31;&#x4E0D;&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x4E86;</li>
</ul>
<p>&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;:
<img src="../imgs/&#x5C40;&#x90E8;&#x53D8;&#x91CF;.png" alt=""></p>
<h2 id="&#x5168;&#x5C40;&#x53D8;&#x91CF;">&#x5168;&#x5C40;&#x53D8;&#x91CF;</h2>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x65E2;&#x80FD;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x4E5F;&#x80FD;&#x5728;&#x5176;&#x4ED6;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x53D8;&#x91CF;&#x5C31;&#x662F;&#x5168;&#x5C40;&#x53D8;&#x91CF;</p>
<p>&#x6253;&#x4E2A;&#x6BD4;&#x65B9;&#xFF1A;&#x6709;2&#x4E2A;&#x5144;&#x5F1F; &#x5404;&#x81EA;&#x90FD;&#x6709;&#x624B;&#x673A;&#xFF0C;&#x5404;&#x81EA;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x5C0F;&#x79D8;&#x5BC6;&#x5728;&#x624B;&#x673A;&#x91CC;&#xFF0C;&#x4E0D;&#x8BA9;&#x53E6;&#x5916;&#x4E00;&#x65B9;&#x4F7F;&#x7528;&#xFF08;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF09;&#xFF1B;&#x4F46;&#x662F;&#x5BB6;&#x91CC;&#x7684;&#x7535;&#x8BDD;&#x662F;2&#x4E2A;&#x5144;&#x5F1F;&#x90FD;&#x53EF;&#x4EE5;&#x968F;&#x4FBF;&#x4F7F;&#x7528;&#x7684;&#xFF08;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF09;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5B9A;&#x4E49;&#x5168;&#x5C40;&#x53D8;&#x91CF;</span>
a = <span class="hljs-number">100</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test1</span><span class="hljs-params">()</span>:</span>
    print(a)  <span class="hljs-comment"># &#x867D;&#x7136;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;a&#x4F46;&#x662F;&#x4F9D;&#x7136;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5176;&#x6570;&#x636E;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test2</span><span class="hljs-params">()</span>:</span>
    print(a)  <span class="hljs-comment"># &#x867D;&#x7136;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;a&#x4F46;&#x662F;&#x4F9D;&#x7136;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5176;&#x6570;&#x636E;</span>

<span class="hljs-comment"># &#x8C03;&#x7528;&#x51FD;&#x6570;</span>
test1()
test2()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;:</p>
<p><img src="../imgs/&#x8BBF;&#x95EE;&#x5168;&#x5C40;&#x53D8;&#x91CF;.png" alt="img"></p>
<h2 id="&#x603B;&#x7ED3;1">&#x603B;&#x7ED3;1:</h2>
<ul>
<li>&#x5728;&#x51FD;&#x6570;&#x5916;&#x8FB9;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x53EB;&#x505A;<code>&#x5168;&#x5C40;&#x53D8;&#x91CF;</code></li>
<li>&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x80FD;&#x591F;&#x5728;&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#x8FDB;&#x884C;&#x8BBF;&#x95EE;</li>
</ul>
<h2 id="&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x548C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x540D;&#x5B57;&#x76F8;&#x540C;&#x95EE;&#x9898;">&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x548C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x540D;&#x5B57;&#x76F8;&#x540C;&#x95EE;&#x9898;</h2>
<p>&#x770B;&#x5982;&#x4E0B;&#x4EE3;&#x7801;:</p>
<p><img src="../imgs/&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x548C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x91CD;&#x540D;.png" alt="img"></p>
<h2 id="&#x603B;&#x7ED3;2">&#x603B;&#x7ED3;2:</h2>
<ul>
<li>&#x5F53;&#x51FD;&#x6570;&#x5185;&#x51FA;&#x73B0;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x548C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x76F8;&#x540C;&#x540D;&#x5B57;&#x65F6;&#xFF0C;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x4E2D;&#x7684; <code>&#x53D8;&#x91CF;&#x540D; = &#x6570;&#x636E;</code> &#x6B64;&#x65F6;&#x7406;&#x89E3;&#x4E3A;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x503C;</li>
</ul>
<h2 id="&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x53D8;&#x91CF;">&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x53D8;&#x91CF;</h2>
<p>&#x51FD;&#x6570;&#x4E2D;&#x8FDB;&#x884C;&#x4F7F;&#x7528;&#x65F6;&#x53EF;&#x5426;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x5462;&#xFF1F;</p>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
<p><img src="../imgs/&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x53D8;&#x91CF;.png" alt="img"></p>
<h2 id="&#x603B;&#x7ED3;3">&#x603B;&#x7ED3;3:</h2>
<ul>
<li>&#x5982;&#x679C;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x51FA;&#x73B0;<code>global &#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x540D;&#x5B57;</code> &#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x5373;&#x4F7F;&#x51FA;&#x73B0;&#x548C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x540D;&#x76F8;&#x540C;&#x7684;<code>&#x53D8;&#x91CF;&#x540D; = &#x6570;&#x636E;</code> &#x4E5F;&#x7406;&#x89E3;&#x4E3A;&#x5BF9;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</li>
<li>&#x5982;&#x679C;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x9700;&#x8981;&#x5BF9;&#x591A;&#x4E2A;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x4E00;&#x6B21;&#x6027;&#x5168;&#x90E8;&#x58F0;&#x660E;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5206;&#x5F00;&#x58F0;&#x660E;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x6B21;global&#x5BF9;&#x591A;&#x4E2A;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x58F0;&#x660E;</span>
<span class="hljs-keyword">global</span> a, b
<span class="hljs-comment"># &#x8FD8;&#x53EF;&#x4EE5;&#x7528;&#x591A;&#x6B21;global&#x58F0;&#x660E;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x7684;</span>
<span class="hljs-comment"># global a</span>
<span class="hljs-comment"># global b</span>
</code></pre>
<h2 id="&#x67E5;&#x770B;&#x6240;&#x6709;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x548C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;">&#x67E5;&#x770B;&#x6240;&#x6709;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x548C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</h2>
<p>Python&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;&#x5185;&#x7F6E;&#x51FD;&#x6570;globals()&#x548C;locals()&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x67E5;&#x770B;&#x6240;&#x6709;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x548C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">()</span>:</span>
    a = <span class="hljs-number">100</span>
    b = <span class="hljs-number">40</span>
    print(locals())  <span class="hljs-comment"># {&apos;a&apos;: 100, &apos;b&apos;: 40}</span>

test()

x = <span class="hljs-string">&apos;good&apos;</span>
y = <span class="hljs-keyword">True</span>
print(globals())  <span class="hljs-comment"># {&apos;__name__&apos;: &apos;__main__&apos;, &apos;__doc__&apos;: None, &apos;__package__&apos;: None, &apos;__loader__&apos;: &lt;_frozen_importlib_external.SourceFileLoader object at 0x101710630&gt;, &apos;__spec__&apos;: None, &apos;__annotations__&apos;: {}, &apos;__builtins__&apos;: &lt;module &apos;builtins&apos; (built-in)&gt;, &apos;__file__&apos;: &apos;/Users/jiangwei/Desktop/Test/test.py&apos;, &apos;__cached__&apos;: None, &apos;test&apos;: &lt;function test at 0x101695268&gt;, &apos;x&apos;: &apos;good&apos;, &apos;y&apos;: True}</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter08/index.html" class="navigation navigation-prev " aria-label="Previous page: 函数高级"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter08/section02.html" class="navigation navigation-next " aria-label="Next page: 函数返回值详解"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
